<template>
  <div class="padding_10">
    <el-button
      v-permissions="{ permission: ['add:GoodsMethod'] }"
      type="primary"
      @click="openTypeDialog"
    >
      新增分组
    </el-button>

    <el-table
      :data="typeList"
      :empty-text="emptyText"
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column label="分组名称" prop="method_name" />
      <el-table-column label="关联类型">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.goods_type_name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分组描述" prop="method_desc" />
      <!-- 自定义表头 -->
      <el-table-column align="left" label="操作">
        <template slot-scope="scope">
          <div class="flex_y_center">
            <el-button
              v-permissions="{ permission: ['edit:GoodsMethod'] }"
              type="primary"
              @click="changeType(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-permissions="{ permission: ['delete:GoodsMethod'] }"
              type="danger"
              @click="delType(scope.row)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div style="display: flex; justify-content: space-between">
      <div></div>
      <el-pagination
        :current-page.sync="page"
        layout="prev, pager, next, jumper"
        :total="total"
        @current-change="getTypeList"
      />
    </div>

    <!-- 新增，编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <div>
        <div class="flex_y_center" style="margin: 15px 0">
          <div style="width: 30%; text-align: right">
            <span>*</span>
            商品分组：
          </div>
          <el-select v-model="formLabelAlign.type_id" clearable>
            <el-option
              v-for="(item, index) of typeArr"
              :key="index"
              :label="item.type_name"
              :value="item.id"
            />
          </el-select>
        </div>
        <div class="flex_y_center">
          <div style="width: 30%; text-align: right">
            <span>*</span>
            商品分组名称：
          </div>
          <el-input
            v-model="formLabelAlign.method_name"
            clearable
            placeholder="请输入商品分组名称"
            style="width: 40%"
          />
        </div>
        <div class="flex_y_center" style="margin: 15px 0">
          <div style="width: 30%; text-align: right">
            <span>*</span>
            商品分组备注：
          </div>
          <el-input
            v-model="formLabelAlign.method_desc"
            clearable
            placeholder="请输入商品分组备注"
            style="width: 40%"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="searchInp">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getCommodityType,
    addCommodityType,
    delCommodityType,
  } from '@/api/commodityType'
  import { getRelatedProducts } from '@/api/commodity'

  export default {
    data() {
      return {
        emptyText: ' ',
        typeList: [], // 存储获取到的商品类型列表信息
        typeArr: [],
        page: 1, // 用作请求商品列表时的分页参数
        total: 10, // 控制分页显示的页码数

        dialogFormVisible: false, // 控制新增，编辑商品类型对话框的显示，隐藏
        dialogTitle: '',

        commodityType: '',
        formLabelAlign: {
          id: '',
          type_id: '',
          method_name: '',
          method_desc: '',
        },
      }
    },
    mounted() {
      this.getTypeList()
      this.getAssociation()
    },
    methods: {
      // 获取商品类型列表
      getTypeList() {
        const loading = this.$loading({
          lock: true,
          target:
            this.typeList.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        const params = {
          page: this.page,
        }
        getCommodityType(params).then((res) => {
          this.typeList = res.data.data
          this.total = res.data.last_page * 10
          loading.close()
          if (res.data.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      // 获取关联商品列表
      getAssociation() {
        getRelatedProducts().then((res) => {
          let mecType
          window.localStorage.getItem('mec_type') == 'school'
            ? (mecType = 'service')
            : (mecType = 'curriculum')

          for (let item of res.data) {
            if (item.type_code != mecType) {
              this.typeArr.push(item)
            }
          }
        })
      },
      // 打开新增商品类型对话框
      openTypeDialog() {
        this.dialogFormVisible = true
        this.dialogTitle = '新增分组'

        for (let item in this.formLabelAlign) {
          this.formLabelAlign[item] = ''
        }
      },
      // 修改商品类型
      changeType(row) {
        this.dialogFormVisible = true
        this.dialogTitle = '编辑分组'

        this.formLabelAlign.id = row.id
        this.formLabelAlign.method_name = row.method_name
        this.formLabelAlign.method_desc = row.method_desc
        this.formLabelAlign.type_id = row.type_id
      },
      // 删除商品类型
      delType(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          // 用户点击确认
          .then(() => {
            delCommodityType(row.id).then(() => {
              this.$message.success(row.method_name + '删除成功!')
              this.getTypeList()
            })
          })
      },
      // 检索form
      searchInp() {
        let arr = []
        const arr1 = {
          type_id: '商品分组',
          method_name: '分组名称',
          method_desc: '分组描述',
        }
        // 循环 formLabelAlign 对象检查除 id 外是否有空值
        for (let item in this.formLabelAlign) {
          // 忽略id不进行判断
          if (item != 'id') {
            if (this.formLabelAlign[item] == '') {
              arr.push(arr1[item])
            }
          }
        }

        // 判断是否检索出为输入的值
        if (arr.length == 0) {
          this.addGoodsType()
        } else {
          this.$message.error(arr + '不崩为空')
        }
        // this.addGoodsType()
      },
      // 新增，修改商品类型
      addGoodsType() {
        addCommodityType(this.formLabelAlign).then(() => {
          this.dialogFormVisible = false
          const hintTxt = this.dialogTitle == '编辑分组' ? '编辑' : '添加'
          this.$message.success(
            this.formLabelAlign.method_name + hintTxt + '成功!'
          )
          this.getTypeList()
        })
      },
    },
  }
</script>

<style scoped>
  span {
    color: red;
  }
</style>
